package cn.nexd.app.config.presenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import cn.nexd.app.config.Config;
import cn.nexd.app.config.R;
import cn.nexd.app.config.ui.activity.SignInActivity;
import cn.nexd.app.config.util.MD5Util;
import cn.nexd.app.config.util.SharedPreferencesUtil;

/**
 * 用户登录校验
 * <p/>
 * Created by wangxu on 16/7/19.
 */
public class SignInPresenter extends BasePresenter<SignInActivity> {

    private static final String TAG = "SignInPresenter";

    private boolean emailValid = false;
    private boolean passwordValid = false;
    private boolean isSavePassword = false;
    private boolean isAutoSignIn = false;

    private static final int AUTO_SIGN_IN = 0;
    private static final int NOT_AUTO_SIGN_IN = 1;

    public SignInPresenter(Context context, SignInActivity iView) {
        super(context, iView);
    }


    //从偏好文件中获取保存的帐号和密码
    public void getAPFromShared() {
        SharedPreferences sharedPreferences = SharedPreferencesUtil.getAccountPasswordAutoSignIn(context);
        String account = sharedPreferences.getString("account", "");
        String password = sharedPreferences.getString("password", "");
        // 获取是否登录
        int isAuto = sharedPreferences.getInt("is_auto", NOT_AUTO_SIGN_IN);

        if (isAuto == AUTO_SIGN_IN) {
            Log.d(TAG, "auto_sign_in");
            iView.setAccountPassword(account, password);
            iView.updateCheckBox(SignInActivity.UPDATE_ALL_CHECKBOX);
            checkEmail(account);
            checkPassword(password);
            signIn(account, password);
        } else {
            if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password)) {
                Log.d(TAG, "reset views what account and password");
                iView.updateCheckBox(SignInActivity.UPDATE_SAVE_CHECKBOX);
                iView.setAccountPassword(account, password);
            }
        }
    }

    public void isSavePassword(boolean isSavePassword) {
        this.isSavePassword = isSavePassword;
    }

    public void isAutoSignIn(boolean isAutoSignIn) {
        this.isAutoSignIn = isAutoSignIn;
    }


    public void signIn(String account, String password) {
        Log.d(TAG, "login check");
        iView.showProgress();
        if (!emailValid || !passwordValid) {
            Log.d(TAG, "sign in failed because the email or password invalid");
            iView.showToast(context.getString(R.string.failed_login));
        } else if (MD5Util.parseStrToMd5L16(account).equals(Config.USER_NAME)
                && MD5Util.parseStrToMd5L16(password).equals(Config.PASSWORD)) {
            Log.d(TAG, "Sign in Success on " + System.currentTimeMillis());
            iView.showToast(context.getString(R.string.msg_sign_success));
            if (isAutoSignIn) {
                Log.d(TAG, "save the email and password in SharedPreferences , and set sign in  mode is auto");
                SharedPreferencesUtil.saveAccountPasswordAutoSignIn(context, account, password, AUTO_SIGN_IN);
            } else if (isSavePassword) {
                Log.d(TAG, "save the email and password in SharedPreferences , and set sign in mode is not auto");
                SharedPreferencesUtil.saveAccountPasswordAutoSignIn(context, account, password, NOT_AUTO_SIGN_IN);
            }
            iView.signInSuccess();
        } else {
            Log.d(TAG, "the email or password is incorrect!");
            iView.showToast(context.getString(R.string.error_incorrect_password));
        }
        iView.hideProgress();
    }

    public void checkPassword(String password) {
        Log.d(TAG, "check password");
        if (TextUtils.isEmpty(password)) {
            iView.setPasswordError(context.getString(R.string.error_null_password));
            passwordValid = false;
        } else {
            iView.setPasswordError(null);
            passwordValid = true;
        }

    }

    public void checkEmail(String account) {
        Log.d(TAG, "Check email");
        if (TextUtils.isEmpty(account)) {
            iView.setEmailError(context.getString(R.string.error_null_email));
            emailValid = false;
        } else if (!account.contains("@") || !account.endsWith(".com")) {
            iView.setEmailError(context.getString(R.string.error_invalid_email));
            emailValid = false;
        } else {
            iView.setEmailError(null);
            emailValid = true;
        }
    }

}
